<template>
  <el-dialog :title="FormData.id?'编辑经纪':'新增经纪'" :visible="dialogVisible" width="70%" @close="closeFn">
    <el-form ref="form" label-width="80px" :model="FormData" :rules="rules">
      <el-form-item label="名称" prop="name">
        <el-input v-model="FormData.name" />
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input v-model="FormData.email" />
      </el-form-item>
      <el-form-item label="手机号码" prop="phone">
        <el-input v-model="FormData.phone" />
      </el-form-item>
      <el-form-item label="地址" prop="address">
        <el-input v-model="FormData.address" />
      </el-form-item>
      <el-form-item label="描述" prop="desc">
        <el-input
          v-model="FormData.desc"
          type="textarea"
          :rows="4"
          class="input"
        />
      </el-form-item>
      <el-form-item label="封面">
        <uptupian ref="refUptupian" class="aaa" />
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="closeFn">取 消</el-button>
      <el-button type="primary" @click="confirmFn">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import uptupian from './uptupian.vue'
import { addAgents, getAssignAgents, emitAgents } from '@/api/agents'
export default {
  components: {
    uptupian
  },
  props: {
    dialogVisible: {
      type: Boolean,
      default: false
    },
    id: {
      type: [Number, String],
      required: true
    }
  },
  data() {
    return {
      FormData: {
        name: '', // 名称
        desc: '', // 描述
        email: '', // 邮箱
        phone: '', // 手机号码
        address: '' // 地址
      },
      image: '',
      rules: {
        name: [
          { required: true, message: '名称不能为空', trigger: 'blur' },
          { min: 2, max: 10, message: '名称长度2~10', trigger: 'blur' }
        ],
        desc: [
          { required: true, message: '描述不能为空', trigger: 'blur' },
          { min: 2, max: 50, message: '描述长度2~50', trigger: 'blur' }
        ],
        email: [
          { required: true, message: '邮箱不能为空', trigger: 'blur' },
          { min: 2, max: 50, message: '名称长度2~50', trigger: 'blur' }
        ],
        phone: [
          { required: true, message: '手机号码不能为空', trigger: 'blur' },
          {
            pattern: /^1[3-9]\d{9}$/,
            message: '手机号码格式有误',
            trigger: 'blur'
          }
        ],
        address: [
          { required: true, message: '地址不能为空', trigger: 'blur' },
          { min: 2, max: 50, message: '地址长度2~50', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 关闭弹窗
    closeFn() {
      this.$emit('update:dialogVisible', false)
      this.FormData = {
        name: '', // 名称
        desc: '', // 描述
        email: '', // 邮箱
        phone: '', // 手机号码
        address: '', // 地址
        image: (this.$refs.refUptupian.fileList = [])
      }
      this.$refs.form.resetFields()
    },
    // 点击确认按钮
    async confirmFn() {
      // 表单验证
      await this.$refs.form.validate()
      // 图片完整地址
      this.image = this.$refs.refUptupian.fileList[0].url
      // console.log(10086, this.image)
      // 调用新增接口 合并参数
      if (this.FormData.id) {
        await emitAgents({ ...this.FormData, image: this.image })
        // 提示
        this.$message.success('编辑成功')
      } else {
        await addAgents({ ...this.FormData, image: this.image })
        // 提示
        this.$message.success('添加成功')
      }
      // 关闭弹窗
      this.closeFn()
      // 通知父组件刷新
      this.$emit('getAgentss')
      // console.log(11111)
    },
    async emitFnFormData(id) {
      // console.log(this.id);
      const res = await getAssignAgents(id)
      // console.log(131313, res);
      this.FormData = res
      console.log(this.FormData)
      // this.$refs.refUptupian.fileList[0].url = res.image
      //  this.$nextTick(() => {
      //    console.log(this.$refs.refUptupian)
      //  })
      this.$refs.refUptupian.fileList.push({
        url: this.FormData.image
      })
    }
  }
}
</script>

<style scoped>
.input {
  width: 600px;
}
</style>
